home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
GREPSTUF
/
GREPGREP.C
< prev
next >
Wrap
Text File
|
1986-10-31
|
4KB
|
241 lines
/*
GrepGrep.c - Grep and Word Count functions
*/
# include "Grep.h"
static long lineNum;
/*
Set the grep state. Val is either 0 (false) or 255 (true).
*/
GrepState (val)
int val;
{
HiliteControl (pauseCtl, val);
HiliteControl (cancelCtl, val);
grepping = ! (Boolean) val;
}
/*
Display lines matching (or not matching) pattern. This is
called to get a line at a time. Mouse clicks control the
state of the pause variable.
*/
GrepLine ()
{
char buf[bufSiz];
if (!paused)
{
if (StreamGetS (buf) == nil)
{
StopGrep ();
}
else
{
++lineNum;
PtoCstr (buf);
if (match (buf) == prtMatches)
{
if (prtLineNum)
{
DisplayLong (lineNum);
DisplayString ("\p: ");
}
CtoPstr (buf);
DisplayString (buf);
DisplayLn ();
}
}
}
}
/*
Cancel any current grep operation.
*/
StopGrep ()
{
if (grepping)
{
CloseStream ();
GrepState (255);
}
}
/*
Start grepping a file
*/
StartGrep ()
{
StopGrep (); /* terminate any ongoing grep operation */
if (!havePat)
GetGrepPat ();
if (/* now */ havePat)
{
if (GetStream ()) /* do grep setup */
{
lineNum = 0;
paused = false;
SetCTitle (pauseCtl, "\pPause");
GrepState (0); /* turn controls on, grepping true */
}
}
}
/*
Word Count
*/
Wc ()
{
register long lines = 0;
long nonEmptyLines = 0;
long words = 0;
register long chars = 0;
register Boolean inToken = false;
register int c, lastc;
for (lastc = '\r'; /* empty */ ; lastc = c)
{
c = StreamGetC ();
if (c == -1)
break; /* eof */
++chars;
switch (c)
{
case '\r':
{
++lines;
if (lastc != '\r')
++nonEmptyLines;
inToken = false;
break;
}
case ' ':
case '\t':
{
inToken = false;
break;
}
default:
{
if (inToken == false)
{
++words;
inToken = true;
}
}
}
}
if (lastc != '\r') /* in case of missing cr on last line */
{
++lines;
++nonEmptyLines;
}
DisplayLong (chars);
DisplayString ("\p Chars, ");
DisplayLong (words);
DisplayString ("\p Words, ");
DisplayLong (lines);
if (streamType == text)
DisplayString ("\p Lines\r");
else
{
DisplayString ("\p Paragraphs (");
DisplayLong (nonEmptyLines);
DisplayString ("\p non-empty)\r");
}
}
/*
Add string to display area. First insert it at the end. Test if
must scroll lines off top to get the new stuff to show up. If yes,
then do the scroll. To keep from filling up the TERec, delete
whatever got scrolled out of view every once in a while. (The number
of lines scrolled off the top to check for is arbitrary - I clobber
stuff after every 25 lines.) To avoid unnecessary redrawing, set to
no clip before doing the delete (which would redraw) and the scroll
back down (which would also redraw).
Also write string to output file, if one is open.
*/
DisplayString (theStr)
StringPtr theStr;
{
register int dispLines; /* number of lines displayable in window */
register int topLines; /* number of lines currently scrolled off top */
register int scrollLines; /* number of lines to scroll up */
register int height;
long len;
Rect r;
len = theStr[0];
height = (**teHand).lineHeight;
TESetSelect (32760L, 32760L, teHand); /* set to insert at end */
TEInsert (theStr+1, len, teHand);
r = (**teHand).viewRect;
dispLines = (r.bottom - r.top) / height;
topLines = (r.top - (**teHand).destRect.top) / height;
scrollLines = (**teHand).nLines - topLines - dispLines;
if (scrollLines > 0) /* must scroll up */
{
TEScroll (0, -height * scrollLines, teHand); /* scroll up */
topLines += scrollLines;
if (topLines > 25) /* keep TERec from filling up */
{
/*
clobber first line(s), and scroll back down to resync what will then
be the first line. Set clipping empty, so that the redraw from the
delete and the scroll down are not shown.
*/
SetRect (&r, 0, 0, 0, 0);
ClipRect (&r);
TESetSelect (0L, (**teHand).lineStarts[topLines], teHand);
TEDelete (teHand);
TEScroll (0, height * topLines, teHand);
ClipRect (&theWind->portRect);
}
}
if (fileOpen)
{
if (FSWrite (outFile, &len, theStr+1) != noErr)
{
Alarm ("\pWrite Error (Closing File)");
FileOutput ();
}
}
}
DisplayLn ()
{
DisplayString ("\p\r");
}
DisplayLong (l)
long l;
{
Str255 s;
NumToString (l, s);
DisplayString (s);
}